/*
 * Created by JFormDesigner on Sun Jun 04 19:18:46 CST 2023
 */

package gui.admin;

import dao.UserDao;
import entity.User;
import gui.admin.tablemodel.UserTableModel;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.*;

/**
 * @author Liubuzhu
 */
public class ManageUserFrame extends JFrame {
    public ManageUserFrame() throws Exception {
        initComponents();
    }

    private void initComponents() throws Exception {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        table1 = new JTable();
        label1 = new JLabel();
        label2 = new JLabel();
        label3 = new JLabel();
        label4 = new JLabel();
        label5 = new JLabel();
        userId = new JTextField();
        userName = new JTextField();
        userPassword = new JTextField();
        cardBalance = new JTextField();
        button1 = new JButton();
        button2 = new JButton();
        button3 = new JButton();
        identityCbox = new JComboBox();

        //======== this ========
        setPreferredSize(new Dimension(1000, 600));
        setTitle("\u7528\u6237\u4fe1\u606f\u7ba1\u7406\u754c\u9762");
        setVisible(true);
        var contentPane = getContentPane();
        contentPane.setLayout(null);

        //---- table1 ----
        table1.setModel(new UserTableModel());
        contentPane.add(table1);
        table1.setBounds(415, 25, 550, 520);

        //---- label1 ----
        label1.setText("\u7528\u6237\u7f16\u53f7");
        label1.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        contentPane.add(label1);
        label1.setBounds(45, 50, 70, 35);

        //---- label2 ----
        label2.setText("\u59d3\u540d");
        label2.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        contentPane.add(label2);
        label2.setBounds(45, 110, 70, 35);

        //---- label3 ----
        label3.setText("\u7528\u6237\u5bc6\u7801");
        label3.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        contentPane.add(label3);
        label3.setBounds(45, 170, 70, 35);

        //---- label4 ----
        label4.setText("\u5361\u4f59\u989d");
        label4.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        contentPane.add(label4);
        label4.setBounds(45, 230, 70, 35);

        //---- label5 ----
        label5.setText("\u7528\u6237\u8eab\u4efd");
        label5.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        contentPane.add(label5);
        label5.setBounds(45, 290, 70, 35);
        contentPane.add(userId);
        userId.setBounds(125, 50, 220, 35);
        contentPane.add(userName);
        userName.setBounds(125, 110, 220, 35);
        contentPane.add(userPassword);
        userPassword.setBounds(125, 170, 220, 35);
        contentPane.add(cardBalance);
        cardBalance.setBounds(125, 230, 220, 35);

        //---- button1 ----修改
        button1.setText("\u4fee\u6539");
        button1.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        contentPane.add(button1);
        button1.setBounds(55, 450, 90, 40);

        //给修改按钮添加点击事件
        button1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                String userIdTemp = userId.getText();
                String userNameTemp = userName.getText();
                String userPasswordTemp = userPassword.getText();
                float cardBalanceTemp = Float.parseFloat(cardBalance.getText());
                int index = identityCbox.getSelectedIndex();
                String identityTemp = null;
                if (index == 0) {
                    identityTemp = "教师";
                } else {
                    identityTemp = "学生";
                }
                User user = new User(userIdTemp,userNameTemp,userPasswordTemp,cardBalanceTemp,identityTemp);
                UserDao userDao = new UserDao();
                boolean result = userDao.updateUser(user);
                if(userId.getText().equals("")){
                    JOptionPane.showMessageDialog(null,"请先选择要修改的用户");
                }else{
                    if(result){
                        JOptionPane.showMessageDialog(null,"修改用户成功");
                        UserTableModel userTableModel = null;
                        try {
                            userTableModel = new UserTableModel();
                        } catch (Exception ex) {
                            throw new RuntimeException(ex);
                        }
                        table1.setModel(userTableModel);
                    }else{
                        JOptionPane.showMessageDialog(null,"修改用户失败");
                    }
                }
            }
        });

        //---- button2 ----删除
        button2.setText("\u5220\u9664");
        button2.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        contentPane.add(button2);
        button2.setBounds(165, 450, 90, 40);

        //给删除按钮添加点击事件
        button2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int index = identityCbox.getSelectedIndex();
                String identity2 = null;
                if (index == 0) {
                    identity2 = "教师";
                } else {
                    identity2 = "学生";
                }
                int res = JOptionPane.showConfirmDialog(null,"你确定要删除该" + identity2 +"吗？","删除确认",JOptionPane.YES_NO_OPTION);
                if(res == 0){
                    if (userId.getText().equals("")){
                        JOptionPane.showMessageDialog(null,"请先选择要删除的" + identity2);
                    }else {
                        User user = new User();
                        user.setUserId(userId.getText());
                        UserDao userDao = new UserDao();
                        try {
                            String msgSting = userDao.deleteUser(user);
                            JOptionPane.showMessageDialog(null,msgSting);
                            UserTableModel userTableModel = new UserTableModel();
                            table1.setModel(userTableModel);
                        } catch (Exception ex) {
                            throw new RuntimeException(ex);
                        }
                    }
                }
            }
        });

        //---- button3 ----取消
        button3.setText("返回");
        button3.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        contentPane.add(button3);
        button3.setBounds(275, 450, 90, 40);

        button3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                dispose();
                try {
                    AdminMainFrame adminMainFrame = new AdminMainFrame();
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        });

        contentPane.add(identityCbox);
        identityCbox.setBounds(125, 285, 220, 40);

        identityCbox.addItem("教师");
        identityCbox.addItem("学生");

        //给表格添加鼠标点击事件
        table1.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                //super.mouseClicked(e);
                int row = table1.getSelectedRow();//获取鼠标当前点击的行下标
                String userIdTemp = (String) table1.getValueAt(row,0);//获取点击行用户编号
                String userNameTemp = (String) table1.getValueAt(row,1);
                String passwordTemp = (String) table1.getValueAt(row,2);
                float cardBalanceTemp = Float.parseFloat((String) table1.getValueAt(row,3));
                String identity = (String) table1.getValueAt(row,4);
                if (identity.equals("教师")){
                    identityCbox.setSelectedIndex(0);
                } else {
                    identityCbox.setSelectedIndex(1);
                }
                userId.setText(userIdTemp);
                userName.setText(userNameTemp);
                userPassword.setText(passwordTemp);
                cardBalance.setText(String.valueOf(cardBalanceTemp));
            }
        });

        {
            // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < contentPane.getComponentCount(); i++) {
                Rectangle bounds = contentPane.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = contentPane.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            contentPane.setMinimumSize(preferredSize);
            contentPane.setPreferredSize(preferredSize);
        }
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JTable table1;
    private JLabel label1;
    private JLabel label2;
    private JLabel label3;
    private JLabel label4;
    private JLabel label5;
    private JTextField userId;
    private JTextField userName;
    private JTextField userPassword;
    private JTextField cardBalance;
    private JButton button1;
    private JButton button2;
    private JButton button3;
    private JComboBox identityCbox;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
    public static void main(String[] args) throws Exception {
        ManageUserFrame manageUserFrame = new ManageUserFrame();
    }
}
